package org.dtrd.modules.archive.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.config.system.Constant;
import org.dtrd.config.system.Constant.IconographyItem;
import org.dtrd.modules.archive.entity.bean.report.IconographyReport;
import org.dtrd.modules.archive.entity.po.DtrdEntRdArchiveIconography;
import org.dtrd.modules.archive.mapper.DtrdEntRdIconographyMapper;
import org.dtrd.modules.archive.service.IDtrdEntRdIconographyService;
import org.dtrd.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 下肢血管病变
 * @author 萧
 * @date 2021-10-01 22:47:27
 **/
@Service
@DS("multi-datasource1")
public class DtrdEntRdIconographyServiceImpl extends ServiceImpl<DtrdEntRdIconographyMapper, DtrdEntRdArchiveIconography> implements IDtrdEntRdIconographyService {

    @Autowired
    private ISysDictService dictService;

    private final static String xray = "footX-ray";

    @Override
    public IconographyReport getPatientIconographyReport(Integer patientId) {
        LambdaQueryWrapper<DtrdEntRdArchiveIconography> wrapper = Wrappers.lambdaQuery(DtrdEntRdArchiveIconography.class)
                .eq(DtrdEntRdArchiveIconography::getPatientId, patientId);
        DtrdEntRdArchiveIconography po = getOne(wrapper);
        IconographyReport report = null;
        if (po != null) {
            report = new IconographyReport();
            // region下肢血管CTA
            Integer cta = po.getExtremityVesselCta();
            if (cta != null) {
                report.setExtremityVesselCta(handleIconographyItem(cta, po.getExtremityVesselCtaOther()));

            }
            // endregion
            // region 下肢血管DSA
            Integer dsa = po.getExtremityVesselDsa();
            if (dsa != null) {
                report.setExtremityVesselDsa(handleIconographyItem(dsa, po.getExtremityVesselDsaOther()));
            }
            // endregion
            // region下肢血管MRI
            Integer mri = po.getExtremityVesselMri();
            if (mri != null) {
                report.setExtremityVesselMri(handleIconographyItem(mri, po.getExtremityVesselMriOther()));
            }
            // endregion
            // region下肢血管B超
            Integer bi = po.getExtremityVesselBi();
            if (bi != null) {
                report.setExtremityVesselBi(handleIconographyItem(bi, po.getExtremityVesselBiOther()));
            }
            // endregion
            // region其他信息
            report.setOther(po.getOther());
            // endregion
            // region足X射线
            String footXray = po.getFootXray();
            if (StrUtil.isNotBlank(footXray)) {
                String footXrayStr = dictService.translateCombineDict(footXray, xray, po.getFootXrayOther());
                report.setFootXray(footXrayStr);
            }
            // endregion
        }
        return report;
    }

    private String handleIconographyItem(Integer bi, String defaultValue) {
        Constant.IconographyItem item = Constant.IconographyItem.valueOf(bi);
        if (item == null || item == IconographyItem.OTHER) {
            return defaultValue;
        } else {
            return item.getName();
        }
    }

    @Override
    public DtrdEntRdArchiveIconography initEntity(Integer patientId, String reportUrl) {
        DtrdEntRdArchiveIconography entity = new DtrdEntRdArchiveIconography().init();
        entity.setPatientId(patientId);
        entity.setIconographyPhotosUrl(reportUrl);

        return entity;
    }
}
